package com.wjjun.utils;

import android.text.TextUtils;

import com.wjjun.basemvpvm.utils.TimeUtils;

import java.util.regex.Pattern;

/**
 * 身份证工具
 */
public class RegexUtils {

    public static final String REGEX_ID_CARD15 = "^[1-9]\\d{5}\\d{2}((0[1-9])|(10|11|12))(([0-2][1-9])|10|20|30|31)\\d{2}$";

    public static final String REGEX_ID_CARD18 = "^[1-9]\\d{5}(18|19|([23]\\d))\\d{2}((0[1-9])|(10|11|12))(([0-2][1-9])|10|20|30|31)\\d{3}[0-9Xx]$";

    public static final String REGEX_MIX_PWD = "^(?![0-9]+$)(?![a-zA-Z]+$)(?!([^(0-9a-zA-Z)]|[\\(\\)])+$)([^(0-9a-zA-Z)]|[\\(\\)]|[a-zA-Z]|[0-9]){6,}$";

    /**
     * 判断是否是身份证
     *
     * @param idCard
     * @return
     */
    public static boolean isIDCard(String idCard) {
        if (TextUtils.isEmpty(idCard)) {
            return false;
        }
        if (idCard.length() == 15) {
            return isIDCard15(idCard);
        } else if (idCard.length() == 18) {
            return isIDCard18(idCard);
        }
        return false;
    }

    public static boolean isIDCard15(String idCard) {
        return isMatch(REGEX_ID_CARD15, idCard);
    }

    public static boolean isIDCard18(String idCard) {
        return isMatch(REGEX_ID_CARD18, idCard);
    }

    public static boolean isMatch(final String regex, final String input) {
        return input != null && input.length() > 0 && Pattern.matches(regex, input);
    }

    public static String getBirthday(String idCard) {
        if (!isIDCard(idCard)) {
            return "";
        }
        String birthday = "";
        if (idCard.length() == 15) {
            birthday = "19" + idCard.substring(6, 8) + "-"
                    + idCard.substring(8, 10) + "-"
                    + idCard.substring(10, 12);
        } else if (idCard.length() == 18) {
            birthday = idCard.substring(6, 10) + "-"
                    + idCard.substring(10, 12) + "-"
                    + idCard.substring(12, 14);
        }
        return birthday;
    }

    public static long getBirthdayTime(String idCard) {
        String birthday = getBirthday(idCard);
        if (TextUtils.isEmpty(birthday)) {
            return 0;
        }

        return TimeUtils.getData(birthday, "yyyy-MM-dd");
    }

    /**
     * 是否是混合的密码, 字母、数字和符号2种或以上组合，6位以上字符
     */
    public static boolean isMixPwd(String pwd){
        return isMatch(REGEX_MIX_PWD, pwd);
    }

}
